class PositionController {

  def index = { redirect(action: list, params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

  def removeVolunteer = {
    def positionInstance = Position.get(params.positionId)
    def userAccountInstance = UserAccount.get(params.userId)
    positionInstance.removeFromVolunteers(userAccountInstance)
    positionInstance.save()
    flash.put("messageVolunteer", "Volunteer ${userAccountInstance.email} removed")
    render(view: "edit", model: [positionInstance: positionInstance])
  }

  def addVolunteer = {
    def positionInstance = Position.get(params.positionId)
    def userAccountInstance = UserAccount.get(params.userId)
    if (positionInstance?.volunteers.contains(userAccountInstance)) {
      render "<div class='message'>You have already volunteered for position ${positionInstance.name}</div>"
    }
    else {
      if (positionInstance.volunteers.size() < positionInstance.requires) {
        positionInstance.addToVolunteers(userAccountInstance)
        positionInstance.save()
        render "<div class='message'>Your enrollment is confirmed for the following position: ${positionInstance.name}</div>"
      }
      else {
        render "Sorry, the position is already filled"
      }
    }
  }

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [positionInstanceList: Position.list(params), positionInstanceTotal: Position.count()]
  }

  def show = {
    def positionInstance = Position.get(params.id)
    if (!positionInstance) {
      flash.message = "Position not found with id ${params.id}"
      redirect(action: list)
    }
    else { return [positionInstance: positionInstance] }
  }

  def showDetails = {
    def positionInstance = Position.get(params.id)
    if (!positionInstance) {
      flash.message = "Position not found with id ${params.id}"
      redirect(action: list)
    }
    else {
      return [positionInstance: positionInstance, administratorId: params.administratorId]
    }
  }

  def delete = {
    def positionInstance = Position.get(params.id)
    if (positionInstance) {
      try {
        positionInstance.delete()
        flash.message = "Position ${params.id} deleted"
        redirect(action: list)
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Position ${params.id} could not be deleted"
        redirect(action: show, id: params.id)
      }
    }
    else {
      flash.message = "Position not found with id ${params.id}"
      redirect(action: list)
    }
  }

  def edit = {
    def positionInstance = Position.get(params.id)
    if (!positionInstance) {
      flash.message = "Position not found with id ${params.id}"
      redirect(action: list)
    }
    else {
      return [positionInstance: positionInstance, eventId: params.eventId]
    }
  }

  def update = {
    def positionInstance = Position.get(params.id)
    if (positionInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (positionInstance.version > version) {

          positionInstance.errors.rejectValue("version", "position.optimistic.locking.failure", "Another user has updated this Position while you were editing.")
          render(view: 'edit', model: [positionInstance: positionInstance])
          return
        }
      }
      positionInstance.properties = params
      positionInstance.validate()
      if (!positionInstance.hasErrors() && positionInstance.save()) {
        flash.put("messagePosition", "Position ${params.name} updated")
        redirect(controller: 'event', action: 'edit', params: [id: params.eventId])
      }
      else {
        render(view: 'edit', model: [positionInstance: positionInstance])
      }
    }
    else {
      flash.message = "Position not found with id ${params.id}"
      redirect(action: edit, id: params.id)
    }
  }

  def create = {
    def positionInstance = new Position()
    positionInstance.properties = params
    return ['positionInstance': positionInstance, eventId: params.eventId]
  }

  def save = {
    def positionInstance = new Position(params)
    positionInstance.validate()
    if (!positionInstance.hasErrors()) {
      positionInstance.save()
      def eventInstance = Event.get(params.eventId)
      eventInstance.addToPositions(positionInstance)
      eventInstance.save()
      flash.put("messagePosition", "Position ${positionInstance.name} created")
      redirect(controller: "event", action: "edit", id: params.eventId)
    }
    else {
      render(view: 'create', model: [positionInstance: positionInstance])
    }
  }
}
